﻿@page "/translation"
@using VectSharp;
@using VectSharp.SVG;

<style>
	table {
		table-layout: fixed;
	}

	td {
		width: 200px;
		padding: 10px;
	}
</style>

<div style="width: 100vw; height: 100vh; position: relative;">
	<div style="width: calc(100% - 400px); height: 100%; position: absolute; top: 0; left: 0; text-align: center">
		<img src="@imgSource" style="max-width: 100%; max-height: 100%; margin-top: 50vh; transform: translate(0, -50%)" />
	</div>

	<div style="width: 400px; height: 100vh; position: absolute; top: 0; right: 0;">
		<table style="margin-top: 50vh; transform: translate(0, -50%)">
			<tr>
				<td>
					<MatNumericUpDownField Label="Translation X"
										   @bind-Value=@p1X
										   Step="1">
					</MatNumericUpDownField>
				</td>
				<td>
					<MatNumericUpDownField Label="Translation Y"
										   @bind-Value=@p1Y
										   Step="1">
					</MatNumericUpDownField>
				</td>
			</tr>
		</table>
	</div>
</div>

@code {
	private double _p1X = 30;
	private double p1X
	{
		get
		{
			return _p1X;
		}

		set
		{
			_p1X = value;
			Render();
		}
	}

	private double _p1Y = 50;
	private double p1Y
	{
		get
		{
			return _p1Y;
		}

		set
		{
			_p1Y = value;
			Render();
		}
	}

	private string imgSource = "";

	protected override Task OnInitializedAsync()
	{
		Render();
		return Task.CompletedTask;
	}

	public void Render()
	{
		Page page = new Page(120, 120);
		Graphics graphics = page.Graphics;

		Font fnt = new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.TimesItalic), 6);

		graphics.Translate(10, 10);
		graphics.Scale(2, 2);

		graphics.StrokePath(new GraphicsPath().MoveTo(0, 0).LineTo(10, 0), Colour.FromRgb(127, 184, 216));
		graphics.FillPath(new GraphicsPath().MoveTo(12, 0).LineTo(9, 2).LineTo(9, -2).Close(), Colour.FromRgb(127, 184, 216));
		graphics.StrokePath(new GraphicsPath().MoveTo(0, 0).LineTo(0, 10), Colour.FromRgb(234, 174, 127));
		graphics.FillPath(new GraphicsPath().MoveTo(0, 12).LineTo(2, 9).LineTo(-2, 9).Close(), Colour.FromRgb(234, 174, 127));
		graphics.FillPath(new GraphicsPath().Arc(0, 0, 1, 0, 2 * Math.PI), Colour.FromRgb(220, 220, 220));
		graphics.FillText(15 - fnt.MeasureText("x").Width * 0.5, 0, "x", fnt, Colour.FromRgb(128, 128, 128), TextBaselines.Middle);
		graphics.FillText(- fnt.MeasureText("y").Width * 0.5, 16, "y", fnt, Colour.FromRgb(128, 128, 128), TextBaselines.Middle);

		Point direction = new Point(p1X, p1Y);
		double modulus = direction.Modulus();
		direction = new Point(direction.X / modulus, direction.Y / modulus);

		double margin = 5;

		graphics.StrokePath(new GraphicsPath().MoveTo(direction.X * margin, direction.Y * margin).LineTo(p1X * 0.5 - direction.X * margin, p1Y * 0.5 - direction.Y * margin), Colour.FromRgb(180, 180, 180), lineDash: new LineDash(2.5, 2.5, 0));

		graphics.Save();
		graphics.Translate(p1X * 0.5 - direction.X * margin, p1Y * 0.5 - direction.Y * margin);
		graphics.Rotate(Math.Atan2(direction.Y, direction.X));
		graphics.FillPath(new GraphicsPath().MoveTo(2, 0).LineTo(-1, 2).LineTo(-1, -2).Close(), Colour.FromRgb(180, 180, 180));

		graphics.Restore();

		graphics.Translate(p1X * 0.5, p1Y * 0.5);

		graphics.StrokePath(new GraphicsPath().MoveTo(0, 0).LineTo(10, 0), Colour.FromRgb(0, 114, 178));
		graphics.FillPath(new GraphicsPath().MoveTo(12, 0).LineTo(9, 2).LineTo(9, -2).Close(), Colour.FromRgb(0, 114, 178));
		graphics.StrokePath(new GraphicsPath().MoveTo(0, 0).LineTo(0, 10), Colour.FromRgb(213, 94, 0));
		graphics.FillPath(new GraphicsPath().MoveTo(0, 12).LineTo(2, 9).LineTo(-2, 9).Close(), Colour.FromRgb(213, 94, 0));
		graphics.FillPath(new GraphicsPath().Arc(0, 0, 1, 0, 2 * Math.PI), Colour.FromRgb(180, 180, 180));
		graphics.FillText(15 - fnt.MeasureText("x").Width * 0.5, 0, "x", fnt, Colours.Black, TextBaselines.Middle);
		graphics.FillText(- fnt.MeasureText("y").Width * 0.5, 16, "y", fnt, Colours.Black, TextBaselines.Middle);

		using (MemoryStream ms = new MemoryStream())
		{
			page.SaveAsSVG(ms);
			ms.Seek(0, SeekOrigin.Begin);

			using (StreamReader sr = new StreamReader(ms))
			{
				this.imgSource = "data:image/svg+xml;base64," + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sr.ReadToEnd()));
			}
		}
	}
}
